package servlet;

import modle.User;
import service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UserChangePwdServlet", urlPatterns = "/user_changepwd")
public class UserChangePwdServlet extends HttpServlet {
    private UserService userService = new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String password = request.getParameter("password");
        String newPassword = request.getParameter("newPassword");
        User user = (User) request.getSession().getAttribute("user");

        if (password.equals(user.getPassword())) {
            user.setPassword(newPassword);
            userService.updatePwd(user);

            request.setAttribute("msg","修改成功!");

            request.getRequestDispatcher("/user_center.jsp").forward(request,response);
        }else {
            request.setAttribute("failMsg","原密码错误，修改失败!");
            request.getRequestDispatcher("/user_center.jsp").forward(request,response);
        }
    }
}
